import { _decorator, Component, Animation, Sprite, UITransform, animation, AnimationClip, Vec3, SpriteFrame } from 'cc';
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager';
import ResourceManager from '../../Runtime/ResourceManager';
import { DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, PLAYER_DIRECTION_ENUM, SHAKE_DIRECTION_ENUM } from '../../Enums';
import EventManager from '../../Runtime/EventManager';
import { PlayerStateMachine } from './PlayerStateMachine';
import { EntityManager } from '../../Base/EntityManager';
import DataManager from '../../Runtime/DataManager';
import { IEntity } from '../../Levels';
import { EnemyManager } from '../../Base/EnemyManager';
const { ccclass, property } = _decorator;



@ccclass('PlayerManager')
export class PlayerManager extends EntityManager {
    targetX: number
    targetY: number
    private readonly speed = 1 / 10
    private isMoving: boolean = false

    async init(params: IEntity) {
        this.fsm = this.addComponent(PlayerStateMachine)
        await this.fsm.init()
        super.init(params)
        this.targetX = this.x
        this.targetY = this.y

        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE, this.inputHandle, this)
        EventManager.Instance.on(EVENT_ENUM.ATTACK_PLAYER, this.onDead, this)


    }

    update() {
        this.updateXY()
        super.update()
    }

    updateXY() {
        if (this.targetX < this.x) {
            this.x -= this.speed
        } else if (this.targetX > this.x) {
            this.x += this.speed
        }
        if (this.targetY < this.y) {
            this.y -= this.speed
        } else if (this.targetY > this.y) {
            this.y += this.speed
        }

        if (Math.abs(this.targetX - this.x) < 0.01 && Math.abs(this.targetY - this.y) < 0.01 && this.isMoving) {
            this.x = this.targetX
            this.y = this.targetY
            this.isMoving = false
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        }
    }

    /**玩家死亡 */
    onDead(type: ENTITY_STATE_ENUM) {
        this.state = type
    }
    /**攻击震动 */
    onAttackShake(type: SHAKE_DIRECTION_ENUM) {
        EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, type)
    }


    inputHandle(inputDirection: PLAYER_DIRECTION_ENUM) {
        if (this.isMoving) {
            return;
        }
        if (this.state === ENTITY_STATE_ENUM.DEATH || this.state === ENTITY_STATE_ENUM.AIR_DEATH || this.state === ENTITY_STATE_ENUM.ATTACK) {
            return
        }
        const id = this.willAttack(inputDirection)
        if (id) {
            EventManager.Instance.emit(EVENT_ENUM.RECORD_STEP)
            this.state = ENTITY_STATE_ENUM.ATTACK
            EventManager.Instance.emit(EVENT_ENUM.ATTACK_ENEMY, id)
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
            EventManager.Instance.emit(EVENT_ENUM.OPEN_DOOR)
            return
        }
        if (this.willBlock(inputDirection)) {
            if (inputDirection === PLAYER_DIRECTION_ENUM.TOP) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.TOP)
            } else if (inputDirection === PLAYER_DIRECTION_ENUM.BOTTOM) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.BOTTOM)
            } else if (inputDirection === PLAYER_DIRECTION_ENUM.LEFT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.LEFT)
            } else if (inputDirection === PLAYER_DIRECTION_ENUM.RIGHT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.RIGHT)
            } else if (inputDirection === PLAYER_DIRECTION_ENUM.TURN_LEFT) {
                if (this.direction === DIRECTION_ENUM.TOP) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.LEFT)
                } else if (this.direction === DIRECTION_ENUM.LEFT) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.BOTTOM)
                } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.RIGHT)
                } else if (this.direction === DIRECTION_ENUM.RIGHT) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.TOP)
                }

            } else if (inputDirection === PLAYER_DIRECTION_ENUM.TURN_RIGHT) {
                if (this.direction === DIRECTION_ENUM.TOP) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.RIGHT)
                } else if (this.direction === DIRECTION_ENUM.LEFT) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.TOP)
                } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.LEFT)
                } else if (this.direction === DIRECTION_ENUM.RIGHT) {
                    EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_DIRECTION_ENUM.BOTTOM)
                }
            }
            return
        }
        this.move(inputDirection)
    }



    /**移动方法 */
    move(inputDirection: PLAYER_DIRECTION_ENUM) {
        EventManager.Instance.emit(EVENT_ENUM.RECORD_STEP)
        if (inputDirection == PLAYER_DIRECTION_ENUM.TOP) {
            this.targetY -= 1
            this.isMoving = true
            this.showSmoke(DIRECTION_ENUM.TOP)
        } else if (inputDirection == PLAYER_DIRECTION_ENUM.BOTTOM) {
            this.targetY += 1
            this.isMoving = true
            this.showSmoke(DIRECTION_ENUM.BOTTOM)
        } else if (inputDirection == PLAYER_DIRECTION_ENUM.LEFT) {
            this.targetX -= 1
            this.isMoving = true
            this.showSmoke(DIRECTION_ENUM.LEFT)
        } else if (inputDirection == PLAYER_DIRECTION_ENUM.RIGHT) {
            this.targetX += 1
            this.isMoving = true
            this.showSmoke(DIRECTION_ENUM.RIGHT)
        } else if (inputDirection == PLAYER_DIRECTION_ENUM.TURN_LEFT) {
            if (this.direction === DIRECTION_ENUM.TOP) {
                this.direction = DIRECTION_ENUM.LEFT
            } else if (this.direction === DIRECTION_ENUM.LEFT) {
                this.direction = DIRECTION_ENUM.BOTTOM
            } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
                this.direction = DIRECTION_ENUM.RIGHT
            } else if (this.direction === DIRECTION_ENUM.RIGHT) {
                this.direction = DIRECTION_ENUM.TOP
            }
            this.state = ENTITY_STATE_ENUM.TURN_LEFT
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)

        } else if (inputDirection === PLAYER_DIRECTION_ENUM.TURN_RIGHT) {
            if (this.direction === DIRECTION_ENUM.TOP) {
                this.direction = DIRECTION_ENUM.RIGHT
            } else if (this.direction === DIRECTION_ENUM.LEFT) {
                this.direction = DIRECTION_ENUM.TOP
            } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
                this.direction = DIRECTION_ENUM.LEFT
            } else if (this.direction === DIRECTION_ENUM.RIGHT) {
                this.direction = DIRECTION_ENUM.BOTTOM
            }

            this.state = ENTITY_STATE_ENUM.TURN_RIGHT
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        }

    }

    showSmoke(type: DIRECTION_ENUM) {
        EventManager.Instance.emit(EVENT_ENUM.SHOW_SMOKE, this.x, this.y, type)
    }


    /**是否会被阻挡 */
    willBlock(inputDirection: PLAYER_DIRECTION_ENUM) {
        const { targetX: x, targetY: y, direction } = this
        const { tileInfo } = DataManager.Instance
        const { mapRowCount: row, mapColumnCount: column } = DataManager.Instance
        const { x: doorX, y: doorY, state: doorState } = DataManager.Instance.door
        const enemies = DataManager.Instance.enemies.filter(enemy => enemy.state !== ENTITY_STATE_ENUM.DEATH)
        const bursts = DataManager.Instance.bursts.filter(burst => burst.state !== ENTITY_STATE_ENUM.DEATH)

        //按钮方向向上
        if (inputDirection === PLAYER_DIRECTION_ENUM.TOP) {
            const playerNextY = y - 1
            if (direction === DIRECTION_ENUM.TOP) {
                if (playerNextY < 0) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }
                const weaponNextY = y - 2
                const nextPlayerTile = tileInfo[x][playerNextY]
                const nextWeaponTile = tileInfo[x][weaponNextY]

                //判断门碰撞
                if (((x === doorX && playerNextY === doorY) || (x === doorX && weaponNextY === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                //判断敌人碰撞
                for (let i = 0; i < enemies.length; i++) {
                    const { x: enemyX, y: enemyY } = enemies[i]
                    if ((x === enemyX && playerNextY === enemyY) || (x === enemyX && weaponNextY === enemyY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                        return true
                    }
                }

                // 判断地裂陷阱
                for (let i = 0; i < bursts.length; i++) {
                    const { x: burstX, y: burstY } = bursts[i]
                    if ((x === burstX && playerNextY === burstY) && (!nextWeaponTile || nextWeaponTile.turnable)) {
                        return false
                    }
                }


                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {

                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                if (playerNextY < 0) {
                    return true
                }
                const weaponNextY = y
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[x]?.[weaponNextY]

                //判断门碰撞
                if (((x === doorX && playerNextY === doorY) || (x === doorX && weaponNextY === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }
                //判断敌人碰撞
                for (let i = 0; i < enemies.length; i++) {
                    const { x: enemyX, y: enemyY } = enemies[i]
                    if (x === enemyX && playerNextY === enemyY) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }
            } else if (direction === DIRECTION_ENUM.LEFT) {
                if (playerNextY < 0) {
                    return true
                }
                const weaponNextX = x - 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门碰撞
                if (((x === doorX && playerNextY === doorY) || (weaponNextX === doorX && weaponNextY === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }
                //判断敌人碰撞
                for (let i = 0; i < enemies.length; i++) {
                    const { x: enemyX, y: enemyY } = enemies[i]
                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                        return true
                    }
                }
                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                if (playerNextY < 0) {
                    return true
                }
                const weaponNextX = x + 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                        return true
                    }
                }

                // 判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }
            }
            //按钮方向向下
        } else if (inputDirection === PLAYER_DIRECTION_ENUM.BOTTOM) {
            const playerNextY = y + 1

            //玩家方向——向上
            if (direction === DIRECTION_ENUM.TOP) {
                if (playerNextY > column - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }

                const weaponNextY = y
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[x]?.[weaponNextY]

                // 判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === x && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }

                // 判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === x && enemyY === playerNextY) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                        return true
                    }
                }

                //  判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }

                //玩家方向——向下
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                if (playerNextY > column - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                const weaponNextY = y + 2
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[x]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === x && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                // 判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === weaponNextY) || (enemyX === x && enemyY === playerNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                //玩家方向——向左
            } else if (direction === DIRECTION_ENUM.LEFT) {
                if (playerNextY > column - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT

                    return true
                }

                const weaponNextX = x - 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //玩家方向——向右
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                if (playerNextY > column - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT

                    return true
                }

                const weaponNextX = x + 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }
            }

            //按钮方向——向左
        } else if (inputDirection === PLAYER_DIRECTION_ENUM.LEFT) {
            const playerNextX = x - 1

            //玩家方向——向上
            if (direction === DIRECTION_ENUM.TOP) {
                //判断是否超出地图
                if (playerNextX < 0) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT

                    return true
                }

                const weaponNextX = x - 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //玩家方向——向下
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                //判断是否超出地图
                if (playerNextX < 0) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT

                    return true
                }

                const weaponNextX = x - 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }

                //玩家方向——向左
            } else if (direction === DIRECTION_ENUM.LEFT) {
                //判断是否超出地图
                if (playerNextX < 0) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT

                    return true
                }

                const weaponNextX = x - 2
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === y)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                //玩家方向——向右
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                //判断是否超出地图
                if (playerNextX < 0) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK

                    return true
                }

                const weaponNextX = x
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === playerNextX && enemyY === y) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }
            }

            //按钮方向——向右
        } else if (inputDirection === PLAYER_DIRECTION_ENUM.RIGHT) {
            const playerNextX = x + 1

            //玩家方向——向上
            if (direction === DIRECTION_ENUM.TOP) {
                if (playerNextX > row - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT

                    return true
                }

                const weaponNextX = x + 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_RIGHT
                    return true
                }

                //玩家方向——向下
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                if (playerNextX > row - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT

                    return true
                }

                const weaponNextX = x + 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_LEFT
                    return true
                }

                //玩家方向——向左
            } else if (direction === DIRECTION_ENUM.LEFT) {
                if (playerNextX > row - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK

                    return true
                }

                const weaponNextX = x
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === playerNextX && enemyY === y) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_BACK
                    return true
                }

                //玩家方向——向右
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                if (playerNextX > row - 1) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT

                    return true
                }

                const weaponNextX = x + 2
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === y)) {
                        this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCK_FRONT
                    return true
                }
            }

            //按钮方向——左转
        } else if (inputDirection === PLAYER_DIRECTION_ENUM.TURN_LEFT) {
            let nextX
            let nextY
            if (direction === DIRECTION_ENUM.TOP) {
                nextX = x - 1
                nextY = y - 1
            } else if (direction === DIRECTION_ENUM.LEFT) {
                nextX = x - 1
                nextY = y + 1
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                nextX = x + 1
                nextY = y + 1
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                nextX = x + 1
                nextY = y - 1
            }

            //判断门碰撞
            if (((x === doorX && nextY === doorY) || (nextX === doorX && y === doorY) || (nextX === doorX && nextY === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {
                this.state = ENTITY_STATE_ENUM.BLOCK_TURN_LEFT
                return true
            }

            //判断敌人碰撞
            for (let i = 0; i < enemies.length; i++) {
                const enemy = enemies[i]
                const { x: enemyX, y: enemyY } = enemy
                if ((x === enemyX && nextY === enemyY) || (nextX === enemyX && y === enemyY) || (nextX === enemyX && nextY === enemyY)) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_TURN_LEFT
                    return true
                }
            }

            //判断地图元素碰撞
            if (
                (!tileInfo[x][nextY] || tileInfo[x][nextY].turnable) &&
                (!tileInfo[nextX][y] || tileInfo[nextX][y].turnable) &&
                (!tileInfo[nextX][nextY] || tileInfo[nextX][nextY].turnable)
            ) {

            } else {
                this.state = ENTITY_STATE_ENUM.BLOCK_TURN_LEFT
                return true
            }
            //按钮方向右转
        } else if (inputDirection === PLAYER_DIRECTION_ENUM.TURN_RIGHT) {
            let nextX, nextY
            if (direction === DIRECTION_ENUM.TOP) {
                //朝上右转的话，右上角三个tile都必须turnable为true
                nextY = y - 1
                nextX = x + 1
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                nextY = y + 1
                nextX = x - 1
            } else if (direction === DIRECTION_ENUM.LEFT) {
                nextY = y - 1
                nextX = x - 1
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                nextY = y + 1
                nextX = x + 1
            }

            //判断门碰撞
            if (((x === doorX && nextY === doorY) || (nextX === doorX && y === doorY) || (nextX === doorX && nextY === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {
                this.state = ENTITY_STATE_ENUM.BLOCK_TURN_RIGHT
                return true
            }


            //判断敌人
            for (let i = 0; i < enemies.length; i++) {
                const enemy = enemies[i]
                const { x: enemyX, y: enemyY } = enemy
                if (enemyX === nextX && enemyY === y) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_TURN_RIGHT
                    return true
                } else if (enemyX === nextX && enemyY === nextY) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_TURN_RIGHT
                    return true
                } else if (enemyX === x && enemyY === nextY) {
                    this.state = ENTITY_STATE_ENUM.BLOCK_TURN_RIGHT
                    return true
                }
            }


            //最后判断地图元素
            if (
                (!tileInfo[x]?.[nextY] || tileInfo[x]?.[nextY].turnable) &&
                (!tileInfo[nextX]?.[y] || tileInfo[nextX]?.[y].turnable) &&
                (!tileInfo[nextX]?.[nextY] || tileInfo[nextX]?.[nextY].turnable)
            ) {
                // empty
            } else {
                this.state = ENTITY_STATE_ENUM.BLOCK_TURN_RIGHT
                return true
            }
        }
        return false


    }

    /**是否会攻击敌人 */
    willAttack(type: PLAYER_DIRECTION_ENUM) {
        const enemies = DataManager.Instance.enemies.filter(
            (enemy: EnemyManager) => enemy.state !== ENTITY_STATE_ENUM.DEATH,
        )
        for (let i = 0; i < enemies.length; i++) {
            const { x: enemyX, y: enemyY, id: enemyId } = enemies[i]
            if (type === PLAYER_DIRECTION_ENUM.TOP && this.direction === DIRECTION_ENUM.TOP && enemyX === this.x && enemyY === this.targetY - 2) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyId
            } else if (type === PLAYER_DIRECTION_ENUM.LEFT && this.direction === DIRECTION_ENUM.LEFT && enemyX === this.x - 2 && enemyY === this.targetY) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyId
            } else if (type === PLAYER_DIRECTION_ENUM.BOTTOM && this.direction === DIRECTION_ENUM.BOTTOM && enemyX === this.x && enemyY === this.targetY + 2) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyId
            } else if (type === PLAYER_DIRECTION_ENUM.RIGHT && this.direction === DIRECTION_ENUM.RIGHT && enemyX === this.x + 2 && enemyY === this.targetY) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyId
            }
        }
        return ''

    }

    onDestroy() {
        super.onDestroy()
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE, this.inputHandle)
        EventManager.Instance.off(EVENT_ENUM.ATTACK_PLAYER, this.onDead)

    }



}


